/*****************************************************************************
 * vlc_open_dialog.c : GTK+ plugin for vlc
 *****************************************************************************/

#include "opendialog.h"

#include <vlc/vlc.h>
#include <vlc_interface.h>
#include <vlc_playlist.h>

#include "vlc_connect.h"
#include "gtk_extra/gtk_extra.h"

/* Widget */

static void vlc_open_dialog_class_init (VlcOpenDialogClass *klass);
static void vlc_open_dialog_init       (VlcOpenDialog *dialog);

G_DEFINE_TYPE (VlcOpenDialog, vlc_open_dialog, GTK_TYPE_DIALOG);

/* Internal */

static GtkWidget* create_open_file_widget (VlcOpenDialog *dialog);
static GtkWidget* create_use_subtitle_widget (void);
static GtkWidget* create_use_more_option_widget (void);

static gchar* create_filter_name_from_extension_suite (const gchar *pattern_list);

/* Callbacks */

static void cb_toggled_check_button_use_subtitle (GtkToggleButton *togglebutton, gpointer user_data);
static void cb_toggled_check_button_more_options (GtkToggleButton *togglebutton, gpointer user_data);
static void cb_clicked_button_browse_subtitle (GtkButton *button, gpointer user_data);
static void cb_clicked_file_chooser_ok (GtkButton *button, gpointer user_data);
static void cb_selection_changed_file_chooser (GtkFileChooser *chooser, gpointer user_data);


static void
vlc_open_dialog_class_init (VlcOpenDialogClass *klass)
{}

static void
vlc_open_dialog_init (VlcOpenDialog *dialog)
{

  GtkWidget *button;
  GtkWidget *check_button;
  gchar *str;
   
  gtk_window_set_title (GTK_WINDOW (dialog), _("Open"));

  dialog->notebook = gtk_notebook_new ();
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), dialog->notebook, TRUE, TRUE, 0);

  gtk_notebook_append_page (GTK_NOTEBOOK (dialog->notebook),\
                            create_open_file_widget (dialog),\
                            gtk_label_new_with_qt_mnemonic_style (_("&File")));

  gtk_notebook_append_page (GTK_NOTEBOOK (dialog->notebook),\
                            gtk_label_new ("Disc pane"),\
                            gtk_label_new_with_qt_mnemonic_style (_("&Disc")));

  gtk_notebook_append_page (GTK_NOTEBOOK (dialog->notebook),\
                            gtk_label_new ("Network pane"),\
                            gtk_label_new_with_qt_mnemonic_style (_("&Network")));
  
  gtk_notebook_append_page (GTK_NOTEBOOK (dialog->notebook),
                            gtk_label_new ("Capture device pane"),\
                            gtk_label_new_with_qt_mnemonic_style (_("Capture &Device")));

  str = qt_to_gtk_mnemonic_string (_("Show &more options"));
  check_button = gtk_check_button_new_with_mnemonic (str);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), check_button, FALSE, FALSE, 4);

  dialog->more_options = create_use_more_option_widget ();
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), dialog->more_options, FALSE, FALSE, 4);

  g_signal_connect (G_OBJECT (check_button), "toggled", G_CALLBACK (cb_toggled_check_button_more_options), dialog->more_options);

  gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
  
  str = qt_to_gtk_mnemonic_string (_("&Play"));
  button = gtk_button_new_with_mnemonic (str);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_file_chooser_ok), dialog);
  g_free (str);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), button, FALSE, FALSE, 0);

  gtk_widget_show_all (GTK_WIDGET (dialog));

}

/* create our widget */

GtkWidget*
vlc_open_dialog_new (GtkWindow *parent, int page_id)
{

  GtkWidget *dialog;

  dialog = g_object_new (VLC_TYPE_OPEN_DIALOG, NULL);
  
  if (parent != NULL)
   {
     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent); 
   }
  
  gtk_widget_hide_all (VLC_OPEN_DIALOG (dialog)->subtitle_frame);
  
  gtk_notebook_set_current_page (GTK_NOTEBOOK (VLC_OPEN_DIALOG (dialog)->notebook), page_id);
    
  return dialog;
  
}

/* Private */

static GtkWidget*
create_use_more_option_widget (void)
{

  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *label;

  frame = gtk_frame_new (NULL);
  
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  
  hbox = gtk_hbox_new (FALSE, 4);
  
  label = gtk_label_new (_("Caching"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  
  label = gtk_label_new (_("Start Time"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  return frame;

}

static void
cb_toggled_check_button_more_options (GtkToggleButton *togglebutton, gpointer user_data)
{

}

static GtkWidget*
create_open_file_widget (VlcOpenDialog *dialog)
{

  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *check_button;
  GtkFileFilter *filter;
  GtkTooltips *tooltips;
  gchar *str;
  
  tooltips = gtk_tooltips_new ();
  
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);

  dialog->chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
  g_signal_connect (GTK_FILE_CHOOSER (dialog->chooser), "file-activated", G_CALLBACK (cb_clicked_file_chooser_ok), dialog);
  gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog->chooser), TRUE);
  gtk_widget_set_size_request (dialog->chooser, 450, 340);

  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Media Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_MEDIA));
                         
  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_MEDIA);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);
  g_free (str);
  
  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Video Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_VIDEO));

  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_VIDEO);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);
  g_free (str);
  
  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Audio Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_AUDIO));
  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_AUDIO);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);
  g_free (str);
  
  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Playlist Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_PLAYLIST));
  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_PLAYLIST);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);
  g_free (str);

  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Subtitles Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_SUBTITLE));
  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_SUBTITLE);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);
  g_free (str);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("All Files ( *.* )"));
  gtk_file_filter_add_pattern (filter, "*");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog->chooser), filter);

  gtk_box_pack_start (GTK_BOX (vbox), dialog->chooser, TRUE, TRUE, 0);

  hbox = gtk_hbox_new (FALSE, 16);

  label = gtk_label_new (_("File Names:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  
  entry = gtk_entry_new ();
  gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
  g_signal_connect (GTK_FILE_CHOOSER (dialog->chooser), "selection-changed", G_CALLBACK (cb_selection_changed_file_chooser), entry);  
  gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
  
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 8);

  dialog->subtitle_frame = create_use_subtitle_widget ();

  str = qt_to_gtk_mnemonic_string (_("Use a sub&titles file"));
  check_button = gtk_check_button_new_with_mnemonic (str);
  gtk_tooltips_set_tip (tooltips, check_button, _("Add a subtitle file"), NULL);
  g_signal_connect (G_OBJECT (check_button), "toggled", G_CALLBACK (cb_toggled_check_button_use_subtitle), dialog->subtitle_frame);
  g_free (str);
  
  gtk_box_pack_start (GTK_BOX (vbox), check_button, FALSE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (vbox), dialog->subtitle_frame, FALSE, FALSE, 0);

  return vbox;

}

static GtkWidget*
create_use_subtitle_widget (void)
{

  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *main_hbox;
  GtkWidget *hbox;
  GtkWidget *combo_box;
  GtkWidget *button;
  GtkWidget *label;
  
  guint i;
  
  static const char *srt_size[] = { N_("Smaller"),
                             N_("Small"),
                             N_("Normal"),
                             N_("Large"),
                             N_("Larger"),
                             NULL};

  static const char *srt_align[] = { N_("Center"),
                              N_("Left"),
                              N_("Right"),
                              NULL};

  frame = gtk_frame_new (NULL);
  
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
  gtk_container_add (GTK_CONTAINER (frame), vbox);  

  hbox = gtk_hbox_new (FALSE, 4);

  combo_box = gtk_combo_box_entry_new ();
  gtk_box_pack_start (GTK_BOX (hbox), combo_box, TRUE, TRUE, 0);
  
  button = gtk_button_new_with_label (_("Browse..."));
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (cb_clicked_button_browse_subtitle), NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  main_hbox = gtk_hbox_new (TRUE, 8);
  hbox = gtk_hbox_new (FALSE, 4);

  label = gtk_label_new (_("Size :"));  
  gtk_misc_set_alignment (GTK_MISC(label), 1.0, 0.5);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

  combo_box = gtk_combo_box_new_text ();
  
  for (i = 0; srt_size[i] != NULL; i++)
   {
     gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), _(srt_size[i]));
   }

  gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 2);
  gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (main_hbox), hbox, TRUE, TRUE, 0);

  hbox = gtk_hbox_new (FALSE, 4);

  label = gtk_label_new (_("Alignment :"));
  gtk_misc_set_alignment (GTK_MISC(label), 1.0, 0.5);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  
  combo_box = gtk_combo_box_new_text ();
  
  for (i = 0; srt_align[i] != NULL; i++)
   {
     gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), _(srt_align[i]));
   }
  
  gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
  gtk_box_pack_start (GTK_BOX (hbox), combo_box, FALSE, FALSE, 0);
  
  gtk_box_pack_start (GTK_BOX (main_hbox), hbox, TRUE, TRUE, 0);

  gtk_box_pack_start (GTK_BOX (vbox), main_hbox, FALSE, FALSE, 0);

  return frame;

}

static gchar*
create_filter_name_from_extension_suite (const gchar *pattern_list)
{
  
  gchar **str_array;
  gchar *str = NULL;
  gchar *cat = NULL;
  int i;

  str_array = g_strsplit (pattern_list, ";", -1);

  str = g_strconcat (str_array[0], ", ", NULL);
  
  for (i = 1; str_array[i] != NULL; i++)
   {
     cat = g_strdup (str);
     
     if (str)
      {
        g_free (str);
      }
      
     if (i == 3)
      {
        str = g_strconcat (cat, _("..."), NULL);
        break;
      }
     else
      {
        str = g_strconcat (cat, str_array[i], ", ", NULL);
      }
    
     if (cat)
      {
        g_free (cat);
      }
   }

  g_free (cat);

  g_strfreev (str_array);

  return str;

}

static void
cb_toggled_check_button_use_subtitle (GtkToggleButton *togglebutton, gpointer user_data)
{
  
  if (gtk_toggle_button_get_active (togglebutton))
   {
     gtk_widget_show_all (GTK_WIDGET (user_data));
   }
  else
   {
     gtk_widget_hide_all (GTK_WIDGET (user_data));
   }
   
}

static void
cb_clicked_button_browse_subtitle (GtkButton *button, gpointer user_data)
{

  GtkWidget *parent;
  GtkWidget *dialog;
  GtkFileFilter *filter;
  gchar *str;

  parent = gtk_widget_get_toplevel (GTK_WIDGET (button));
  
  dialog = gtk_file_chooser_dialog_new (_("Open subtitles file"),\
                                        GTK_WINDOW (parent),\
                                        GTK_FILE_CHOOSER_ACTION_OPEN,\
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,\
                                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,\
                                        NULL);

  filter = gtk_file_filter_new ();
  str = g_strdup_printf ("%s ( %s )",\
                         _("Subtitles Files"),\
                         create_filter_name_from_extension_suite (EXTENSIONS_SUBTITLE));
  gtk_file_filter_set_name (filter, str);
  gtk_file_filter_add_multiple_pattern (filter, EXTENSIONS_SUBTITLE);
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
  g_free (str);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("All Files ( *.* )"));
  gtk_file_filter_add_pattern (filter, "*");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);

  if (gtk_dialog_run (GTK_DIALOG (dialog)) ==  GTK_RESPONSE_ACCEPT)
   {
   }

  gtk_widget_destroy (dialog);

}

static void
cb_selection_changed_file_chooser (GtkFileChooser *chooser, gpointer user_data)
{

  GSList *filenames = NULL;
  GSList *i;
  GString *string;
  gchar *str;
  gchar *base_name;

  string = g_string_new (NULL);

  filenames = gtk_file_chooser_get_filenames (chooser);

  if (g_slist_length (filenames) > 1)
   {
   
    for (i = filenames; i != NULL; i = i->next)
     {
     
      if (!g_file_test (i->data, G_FILE_TEST_IS_DIR))
       {
        base_name = g_filename_display_basename (i->data);
        str = g_strconcat ("\"", base_name, "\" ", NULL);
        g_string_append (string, (const gchar *)str);
        g_free (str);
        g_free (base_name);
       }
    
     }
   
   }

  else if (filenames != NULL)
   {
    if (!g_file_test (filenames->data, G_FILE_TEST_IS_DIR))
     {
      base_name = g_filename_display_basename (filenames->data);
      g_string_append (string, (const gchar *)base_name);    
      g_free (base_name);
     }
   }

  gtk_entry_set_text (GTK_ENTRY (user_data), string->str);

  g_string_free (string, TRUE);

  g_slist_foreach (filenames, (GFunc) g_free, NULL);
  g_slist_free (filenames);

}

static void
cb_clicked_file_chooser_ok (GtkButton *button, gpointer user_data)
{

  VlcOpenDialog *dialog;

  GSList *filenames;
  GSList *i;
  gchar *uri;
  gboolean start = TRUE;

  input_item_t *p_input;

  dialog = VLC_OPEN_DIALOG (user_data);

  if (!is_file_in_playlist ())
   {
    playlist_Clear (VLC_PLAYLIST, VLC_FALSE);
   }

  filenames = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog->chooser));

  for (i = filenames; i != NULL; i= i->next)
   {
    
    uri = g_filename_to_uri (i->data, NULL, NULL);
    
    if (!g_file_test (i->data, G_FILE_TEST_IS_DIR))
     {
      p_input = input_ItemNew (VLC_INTF_THREAD, uri, NULL);
      
      if (start)
       {
        playlist_AddInput (VLC_PLAYLIST, p_input,
                           PLAYLIST_APPEND | PLAYLIST_GO,
                           PLAYLIST_END, VLC_TRUE, VLC_FALSE);
        start = FALSE;
       }
      else
       {
        playlist_AddInput (VLC_PLAYLIST, p_input,
                           PLAYLIST_APPEND | PLAYLIST_PREPARSE,
                           PLAYLIST_END, VLC_TRUE, VLC_FALSE);
       
       }

     }

    if (uri != NULL)
     {
      g_free (uri);
     }

    }

  g_slist_foreach (filenames, (GFunc) g_free, NULL);
  g_slist_free (filenames);

  gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  
}

